package com.hkts.naturalgas.dictionaries.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;

/**
 * @ClassNameEasyExcelHelper
 * @Description
 * @Author yck
 * @Date27/1/2022 上午9:33
 * @Version V1.0
 **/
@Data
public class EasyExcelHelper {
    private ExcelWriter excelWriter;

    private ExcelMergeStrategy excelMergeStrategy;

    public EasyExcelHelper(String path) {
        excelWriter = EasyExcel.write(path).build();
    }

    /**
     * 动态插入
     *
     * @param sheetStartNum
     * @param sheetName     sheet名称
     * @param headList      动态标题，格式：List<List<String>>
     * @param list          数据
     */
/*    public void createSheet(Integer sheetStartNum, String sheetName, List headList, List list) {
        WriteSheet sheet;
        if (Objects.isNull(excelMergeStrategy)) {
            sheet = EasyExcel.writerSheet(sheetStartNum, sheetName).
                    registerWriteHandler(defaultStyle()).
                    registerWriteHandler(new CustomerColumnWidthStyleStrategy()).build();
        } else {
            sheet = EasyExcel.writerSheet(sheetStartNum, sheetName).
                    registerWriteHandler(defaultStyle()).
                    registerWriteHandler(excelMergeStrategy).
                    registerWriteHandler(new CustomerColumnWidthStyleStrategy()).build();
        }

        WriteTable table = new WriteTable();
        table.setTableNo(0);
        table.setHead(headList);
        excelWriter.write(list, sheet, table);
    }*/

    /**
     * 创建合并策略
     *
     * @param mergeRowIndex    合并开始行 （从1开始，忽略标题）
     * @param mergeColumnIndex 合并列（列下标，从0开始）
     * @param eachRow          合并间隔，指定几条数据之间合并
     */
    public void mergeStrategy(int mergeRowIndex, int[] mergeColumnIndex, int eachRow) {
        excelMergeStrategy = new ExcelMergeStrategy(mergeRowIndex, mergeColumnIndex, eachRow);
    }

    /**
     * 设置头部样式
     *
     * @return
     */
    private HorizontalCellStyleStrategy defaultStyle() {
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //头部样式
        headWriteCellStyle.setFillForegroundColor(IndexedColors.LIME.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteFont.setFontName("Arial");
        headWriteFont.setBold(Boolean.FALSE);
        headWriteFont.setColor(IndexedColors.WHITE.getIndex());
        headWriteCellStyle.setWriteFont(headWriteFont);
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headWriteCellStyle.setWrapped(Boolean.FALSE);
        headWriteCellStyle.setBorderRight(BorderStyle.NONE);
        headWriteCellStyle.setBorderLeft(BorderStyle.NONE);
        //内容样式
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }

}
